package com.sl.au.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.MultipartRequest;
import org.springframework.web.servlet.ModelAndView;

import com.sl.au.entity.ArchiveInfo;
import com.sl.au.entity.EventRegister;
import com.sl.au.entity.ViewForEventRegisterTask;
import com.sl.au.entity.ViewForTimeOutTask;
import com.sl.au.entity.EventReport;
import com.sl.au.entity.EventTask;
import com.sl.au.entity.Module;
import com.sl.au.entity.Option;
import com.sl.au.entity.TimeOutReceiveRegister;
import com.sl.au.entity.User;
import com.sl.au.service.ArchiveInfoService;
import com.sl.au.service.EventRegisterService;
import com.sl.au.service.EventReportService;
import com.sl.au.service.OptionService;
import com.sl.au.service.TimeOutTaskService;
import com.sl.utils.MsgHelper;

@RestController
@RequestMapping("/ui/timeOutDeal")
public class TimeOutDealController extends BaseController {

	@Autowired
	private OptionService optionService;
	@Autowired
	EventReportService eventReportService;
	@Autowired
	private TimeOutTaskService timeOutTaskService;

	/**
	 * 初始化事件信息列表
	 * 
	 * @return
	 */

	@RequestMapping(value = "/initPage", method = RequestMethod.POST)
	public Map<String, Object> initPage(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String key = request.getParameter("key");
		String type = request.getParameter("type");
		List<Option> options=this.optionService.getRepository().findByGroupAndTitle("内部事件流转状态", "处理中");
		String taskStateId = "";
		if(options.size()>0){
			taskStateId=options.get(0).getId();
		}
//		List<TimeOutReceiveRegister> infos = null;
//		int count = 0;
//		infos = timeOutTaskService.getTimeOutTaskForDeal(start, length, taskStateId, type, key, "", null);
//		count = timeOutTaskService.getTimeOutTaskForDealCount(taskStateId, type, key);

		String roleId = this.getMyAccount().getRole().getId();
		List<Map<String, Object>> infos = null;
		int count = 0;
		infos = timeOutTaskService.getTimeOutTaskForDealByRole(start, length, roleId, taskStateId, type, key, "createTime", "desc");
		count = timeOutTaskService.getTimeOutTaskForDealCountByRole(roleId, taskStateId, type, key);

		map.put("data", infos);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}
	
	@RequestMapping("/getProcedure/{eventId}")
	public MsgHelper getProcedure(@PathVariable("eventId") String eventId) {
		List<ViewForTimeOutTask> procedures = timeOutTaskService.getRepository().findTaskProcedures(eventId);
		return MsgHelper.success().put("procedures", procedures);
	}

	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView index(ModelAndView modelAndView) {
		modelAndView = createLayoutView("timeOut/dealIndex");
		Module module = this.getPageModule("TimeOutDeal");
		String type = request.getParameter("type");
		if(!StringUtils.hasText(type)) {
			type = "";
		}
		modelAndView.addObject("module", module);
		modelAndView.addObject("type", type);
		return modelAndView;
	}
}
